import axios from "axios";
// 本地测试
export const URL = process.env.VUE_APP_BASE_API_URL
// 线上数据
// export const URL = 'http://dangjian.csxlib.com/api'

export function getAction(url,config = {}){
    config.headers = config.headers || {}
    const token = localStorage.getItem('token')
    if(token){
        config.headers['X-User-Token'] = token
    }
    return axios.get(url,config).then(res => {
        return res.data
    })
}

export function postAction(url, data, config = {}){
    config.headers = config.headers || {}
    const token = localStorage.getItem('token')
    if(token){
        config.headers['X-User-Token'] = token
    }
    return axios.post(url, data, config).then(res => {
        return res.data
    })
}

/**
 * 栏目页 - 获取栏目数据
 * @returns {Promise<AxiosResponse<any>>}
 */
export function getSideBarData(){
    return getTopicChild('items')
}

/**
 * 获取某一栏目信息
 * @param topicCode
 * @returns {Promise<AxiosResponse<any>>}
 */
export function getTopic(topicCode){
    return getAction(`${URL}/party/open/topic/${topicCode}`)
}

/**
 * 获取某一栏目分类下的子分类
 * @param topicCode
 * @returns {Promise<AxiosResponse<any>>}
 */
export function getTopicChild(topicCode){
    return getAction(`${URL}/party/open/topic/${topicCode}/child`)
}

/**
 * 获取某一栏目下-专辑分页数据
 * @param topicCode
 * @param params
 * @returns {Promise<AxiosResponse<any>>}
 */
export function getTopicPageData(topicCode,params = {}){
    return getAction(`${URL}/party/open/topic/${topicCode}/page`,{params})
}

/**
 * 获取专辑内容
 */
export function getResource(id,params = {}){
    return getAction(`${URL}/party/open/res/${id}`,{params})
}

/**
 * 获取某栏目中首个专辑资源
 */
export function getFirstResourceInTopic(topicCode,params = {}){
    return getTopicPageData(topicCode,{pageSize:1}).then(res => {
        const result = res.result
        if(res.code === 500 || !result){
            throw new Error('栏目不存在')
        }
        const records = result.records
        if(!records || records.length < 0){
            return new Error('没有找到资源')
        }
        return getResource(records[0].id,params)
    })
}

/**
 * 用户登录
 */
export function doLoginByPwd(data){
    return postAction(`${URL}/party/open/user/login`,data)
}

/**
 * 用户登录
 */
export function doLoginBySmsCode(data){
    return postAction(`${URL}/party/open/user/login`,data)
}

/**
 * 注册
 * @param data
 */
export function doRegister(data){
    return postAction(`${URL}/party/open/user/register`,data)
}

/**
 * 获取验证码图片
 */
export function getCaptcha(key) {
    return getAction(`${URL}/party/open/user/captcha/${key}`)
}

/**
 * 发送图形验证码
 * @param phone
 * @param captcha
 */
export function sendSmsCode(phone,captcha){
    return postAction(`${URL}/party/open/user/sms-code?phone=${phone}`,captcha)
}

/**
 * 获取用户信息
 */
export function getUserinfo(){
    return getAction(`${URL}/party/open/user/userinfo`)
}

/**
 * 用户签到
 */
export function signIn(){
    return postAction(`${URL}/party/open/user/sign-in`)
}

/**
 * 更新用户信息
 */
export function doUpdate(data){
    return postAction(`${URL}/party/open/user/userinfo`,data)
}

/**
 * 获取用户当日积分
 */
export function getPointTody() {
    return getAction(`${URL}/party/open/user/point/simple`)
}

/**
 * 获取积分规则
 */
export function getPointRule() {
    return getAction(`${URL}/party/open/user/point/detail`)
}

/**
 * 获取历时积分数据
 */
export function getPointHistory(params = {pageNum: 1}) {
    return getAction(`${URL}/party/open/user/point/history`,{params})
}

/**
 * 搜索
 */
export function search(params = {}) {
    return getAction(`${URL}/party/open/common/search`,{params})
}

/**
 * 获取积分商场分页数据
 */
export function getGoodsByTopic(params = {}) {
    return getAction(`${URL}/party/open/point-market/page`,{params})
}

/**
 * 增加积分
 */
export function pointCallback(resourceId) {
    return getAction(`${URL}/party/open/res/${resourceId}/callback`)
}

/**
 * 获取商品信息
 */
export function getGoodsInfo(id) {
    return getAction(`${URL}/party/open/point-market/${id}`)
}

/**
 * 兑换商品
 */
export function order(data) {
    return postAction(`${URL}/party/open/order/`, data)
}

/**
 * 获取订单详情
 */
export function getOrderInfo(id) {
    return getAction(`${URL}/party/open/order/${id}`)
}

/**
 * 获取订单列表
 */
export function getOrderList(params = {}) {
    return getAction(`${URL}/party/open/order/page`, {params})
}
